package com.lixin.smartclassroom.ui.login

import android.graphics.Color
import android.os.StrictMode
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.drake.net.time.Interval
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.getCode
import com.lixin.smartclassroom.api.phoneCodeLogin
import com.lixin.smartclassroom.api.pwdLogin
import com.lixin.smartclassroom.app.App
import com.lixin.smartclassroom.bean.RoleBean
import com.lixin.smartclassroom.databinding.ActivityLoginBinding
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.MainActivity
import com.lixin.smartclassroom.ui.home.adapter.selectRole
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.isFirstOpen
import com.lixinkeji.extension.isLogin
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.token
import com.lixinkeji.extension.visible
import java.util.concurrent.TimeUnit

/**
 * Description
 * Author: lwc
 * Date:2023/12/4 15:54
 */
class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    override fun getViewBinding() = ActivityLoginBinding.inflate(layoutInflater)
    override fun topBarHide() = true
    override fun startPadding() = false
    private var loginType = 1;//0验证码登录  1密码登录

    private var phoneNum = ""
    private var phoneCode = ""
    private var password = ""
    var listBean: ArrayList<RoleBean>? = null
    lateinit var interval: Interval
    private var selectRole: RoleBean? = null


    private fun getRoleData() {
        listBean = ArrayList()
        listBean?.add(RoleBean(3, R.drawable.role_parents, "我是家长", false))
        listBean?.add(RoleBean(1, R.drawable.role_student, "我是学生", false))
        listBean?.add(RoleBean(2, R.drawable.role_teacher, "我是老师", false))
    }

    override fun iniView() {
        getRoleData()
        binding.apply {
            recycler.selectRole().apply {
                R.id.rl.onClick {
                    llLogin.visible()

                    selectRole = listBean?.get(modelPosition)
                    if (selectRole?.roleId == 3) {
                        llParent.visible()
                    } else {
                        llParent.gone()
                    }
                    listBean?.forEach {
                        it.selectFlag = false
                    }
                    listBean?.get(modelPosition)?.selectFlag = true
                    bindingAdapter?.notifyDataSetChanged()
                    setPwdLoginUi()
                }
            }.models = listBean
        }
        initSdk()
    }


    private fun initSdk() {
        App.init(this)
        App.initSdk()

        isFirstOpen = false

        binding.apply {
            inputPhone.paint.isFakeBoldText = true
            inputCode.paint.isFakeBoldText = true
        }

        val builder: StrictMode.VmPolicy.Builder = StrictMode.VmPolicy.Builder()

        StrictMode.setVmPolicy(builder.build())

        builder.detectFileUriExposure()

    }


    override fun onClick() {
        binding.apply {
            tvLoginType.onClick {
                if (loginType == 1) {
                    setPhoneLoginUi()
                } else {
                    setPwdLoginUi()
                }
            }

            tvRegister.onClick {
                start(RegisterAct::class.java)
            }

            imgEyes.onClick {
                if (loginType == 0) {
                    loginType = 1;
                    inputCode.transformationMethod = HideReturnsTransformationMethod
                        .getInstance();
                    imgEyes.setImageDrawable(getDrawable(R.drawable.eye_no))
                } else {
                    loginType = 0;
                    inputCode.transformationMethod = PasswordTransformationMethod
                        .getInstance();
                    imgEyes.setImageDrawable(getDrawable(R.drawable.eye_yes))
                }
            }



            loginBtn.onClick {
                if (selectRole != null) {
                    when (selectRole?.roleId!!) {
                        1 -> user_role = 2  //学生
                        2 -> user_role = 3  //老师
                        3 -> user_role = 1  //家长
                    }
                    if (inputCode.text().isEmpty()) {
                        if (loginType == 1) {
                            toast("请输入密码")
                        } else {
                            toast("请输入验证码")
                        }
                        return@onClick
                    }

                    if (inputPhone.text().isEmpty()) {
                        toast("请输入手机号")
                        return@onClick
                    }

                    scopeNetLife {
                        if (loginType == 1) {
                            val basebean =
                                pwdLogin(
                                    inputPhone.text(),
                                    inputCode.text(),
                                    selectRole?.roleId.toString()
                                )
                            if (basebean.result == "0") {
                                isLogin = true
                                token = basebean.apptoken!!
                                start(MainActivity::class.java)
                            } else {
                                toast(basebean.resultNote.toString())
                            }
                        } else {
                            val basebean =
                                phoneCodeLogin(
                                    inputPhone.text(),
                                    inputCode.text()
                                )
                            if (basebean.result == "0") {
                                isLogin = true
                                token = basebean.apptoken!!
                                start(MainActivity::class.java)
                            } else {
                                toast(basebean.resultNote.toString())
                            }
                        }
                    }

                } else {
                    toast("请选择角色")
                    return@onClick
                }

            }

            sendCode.onClick {
                scopeNetLife {
                    phoneNum = inputPhone.text?.trim().toString()
                    if (phoneNum.isNullOrEmpty()) {
                        toast("请输入手机号")
                        return@scopeNetLife
                    }
                    sendCode.isClickable = false
                    interval = Interval(1, 1, TimeUnit.SECONDS, 60).life(this@LoginActivity)
                    interval.subscribe {
                        sendCode.text = it.toString()
                    }.finish {
                        sendCode.text = "发送验证码" // 最后一位数时同时回调 subscribe/finish
                        sendCode.isClickable = true
                    }.start()
                    getCode(phoneNum)
                }
            }
        }
    }


    private fun setPwdLoginUi() {
        binding.apply {
            tvLoginType.setTextColor(Color.parseColor("#666666"))
            loginType = 1
            inputCode.hint = "请输入密码"
            tvLoginType.text = "验证码登录"
            inputCode.transformationMethod = PasswordTransformationMethod
                .getInstance()
            inputCode.setText("")
            sendCode.gone()
            imgEyes.visible()
        }
    }




    private fun setPhoneLoginUi() {
        binding.apply {
            loginType = 0
            inputCode.hint = "请输入验证码"
            tvLoginType.text = "密码登录"
            inputCode.setText("")
            sendCode.visible()
            imgEyes.gone()
        }
    }
}